package com.st.bottomnavigation.ui.navbar;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.st.bottomnavigation.R;

import java.util.Locale;

/**
 * 主页
 */
public class HomeFragment extends Fragment implements View.OnClickListener {
    private static HomeFragment homeFragment;
    private View rootView;
    private TextToSpeech tts;//创建语音对象
    public HomeFragment() {
    }
    //单例创建
    public static HomeFragment getInstance(){
        if (homeFragment == null) {
            return new HomeFragment();
        }
        Log.d("test", "onCreateView:HomeFragment ");
        return homeFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(R.layout.fragment_home, container, false);
            initView();
        }
        return rootView;

    }

    private void initView() {
        Button voiceBroadcast = rootView.findViewById(R.id.voiceBroadcast);
        voiceBroadcast.setOnClickListener(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.d("onDetach", "onDetach: HomeFragment");
    }

    @Override
    public void onClick(View view) {
        tts=new TextToSpeech(getActivity(), new TextToSpeech.OnInitListener() {//实例化自带语音对象
            @Override
            public void onInit(int i) {
                if(i==TextToSpeech.SUCCESS){//设置语音
                    tts.setLanguage(Locale.CHINESE);//中文
                    tts.speak("今天温度36.3℃",TextToSpeech.QUEUE_FLUSH,null);//播报“今天温度36.3℃”
                }
            }
        });
    }
}